//logs.js
var formatFloat = require('../../utils/mathUtil.js')
var app = getApp()
var host = app.globalData.host
Page({
  data: {
    openId: '',
    shopcarcontent: '',
    selectedStatus: '',
    image: app.globalData.image,
    totalNum: 0,
    totalPrice: 0.0,
    array:[],
    keyArray:[],
    index:0,
    addressId:'',
    remarks:'',
    pageempty: '',
    pageunempty: '',
    recipientName: '',
    recipientMobile: '',
    recipientAddress: '',
    addrId: '',
    recipientName: '',
    recipientMobile: '',
    recipientAddress: ''
  },
  changeRemarks: function (e) {
    var that = this
    this.setData({
      remarks: e.detail.value
    })
  },
  bindPickerChange: function (e) {
    var that = this
    this.setData({
      index: e.detail.value,
      addressId: that.data.keyArray[e.detail.value]
    })
  },
  onShow: function () {
    var that = this;
    var openId = wx.getStorageSync('openId');
    this.setData({
      openId: openId
    })
    var selectAddr = wx.getStorageSync("selectedAddr")
    if (selectAddr) {
      wx.removeStorageSync("selectedAddr")
      console.info("remove storaage")
      this.setData({
        pageempty: '',
        pageunempty: true,
        addrId: selectAddr.id,
        recipientName: selectAddr.name,
        recipientMobile: selectAddr.mobile,
        recipientAddress: selectAddr.province + selectAddr.city + selectAddr.county + selectAddr.address
      })
    } else {
      wx.request({
        url: host + "/addressApi/selAddress",
        data: {
          thirdpartyId: wx.getStorageSync('openId')
        },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          var addrArr = res.data.data
          if (addrArr && addrArr.length > 0) {
            var defaultAddr = addrArr[0];//默认地址
            if (defaultAddr) {
              that.setData({
                pageempty: '',
                pageunempty: true,
                addrId: defaultAddr.id,
                recipientName: defaultAddr.name,
                recipientMobile: defaultAddr.mobile,
                recipientAddress: defaultAddr.province + defaultAddr.city + defaultAddr.county + defaultAddr.address
              })
            } else {
              that.setData({
                pageempty: true,
                pageunempty: '',
                addrId: '',
                recipientName: '',
                recipientMobile: '',
                recipientAddress: ''
              })
            }
          } else {
            that.setData({
              pageempty: true,
              pageunempty: '',
              addrId: '',
              recipientName: '',
              recipientMobile: '',
              recipientAddress: ''
            })
          }
        }
      })
    }

    wx.request({
      url: host + '/cart/list',
      header: { 'content-type': 'application/json' },
      data: {
        openId: openId
      },
      success: function (res) {
        var list = res.data.data.list
        var checkedArr = []
        var allNum = 0
        var allPrice = 0.0
        for (var z = 0; z < list.length; z++) {
          if (list[z].note == "checked") {
            allNum += list[z].skuNum
            allPrice += (list[z].sku.price * list[z].skuNum)
            checkedArr.push(list[z])
          }
          allPrice = formatFloat.formatFloat(allPrice, 2)

        }
        that.setData({
          shopcarcontent: checkedArr,
          totalNum: allNum,
          totalPrice: allPrice
        })

      }
    })
  },
  topay: function (e) {
    var that = this
    if (that.data.pageempty == true){
      wx.showToast({
        title: '请先添加收货地址',
        icon: 'success',
        duration: 2000
      })
    }else{
      wx.request({
        url: host + '/cart/list',
        header: { 'content-type': 'application/json' },
        data: {
          openId: wx.getStorageSync('openId')
        },
        success: function (res) {
          var list = res.data.data.list
          var skuIds = ''
          var skuNums = ''

          var allPrice = 0.0
          for (var z = 0; z < list.length; z++) {
            if (list[z].note == "checked") {
              var skuId = list[z].sku.id
              var skuNum = list[z].skuNum
              skuIds += (skuId + ',')
              skuNums += (skuNum + ',')
            }
          }
          wx.request({
            url: host + '/order/addOrderFromCart',
            header: { 'content-type': 'application/json' },
            data: {
              openId: wx.getStorageSync('openId'),
              skuIds: skuIds,
              productNums: skuNums,
              remarks: that.data.remarks,
              addressId: that.data.addrId
            },
            success: function (res) {
              var source = res.data.data
              var id = source.orderId
              wx.setStorageSync("orderId", id)
              wx.requestPayment({
                'timeStamp': source.timeStamp,
                'nonceStr': source.nonce_str,
                'package': source.package,
                'signType': source.signType,
                'paySign': source.paySign,
                'success': function (res) {
                },
                'fail': function (res) {
                },
                'complete': function (res) {
                  var id = wx.getStorageSync("orderId")
                  wx.removeStorageSync("orderId")
                  wx.redirectTo({
                    url: '../orderdetail/orderdetail?id=' + id,
                  })

                }
              })

              
            }
          });
        }
      });
    }
  },
  toaddress: function () {
    wx.navigateTo({
      url: '../personaddress/personaddress?comefrom=cartorder',
    })
  }
})
